import 'package:flutter/material.dart';
import 'package:ideology/features/screens/widgets/editor_menu_bar/setting/right/download_tab/download_tab.dart';
import 'package:ideology/features/screens/widgets/editor_menu_bar/setting/right/download_tab/libraries_tab_full.dart';
import 'package:ideology/features/screens/widgets/editor_menu_bar/setting/sdk_settings_controller.dart';

/// 构建设置面板右侧的内容区域。
///
/// 包含两个主要 Tab：
///
/// - **Libraries**：展示当前已安装的 SDK 列表，支持切换/删除/定位 SDK。
/// - **Download & Install**：展示可供下载的 SDK 版本，以及下载、安装相关的操作。
///
/// 使用 [SdkSettingsController] 来获取和管理状态。
class BuildRightPane extends StatelessWidget {
  const BuildRightPane({
    super.key,
    required this.controller,
  });

  /// SDK 设置控制器，用于提供数据和操作回调。
  final SdkSettingsController controller;

  @override
  Widget build(BuildContext context) {
    final cs = Theme.of(context).colorScheme;

    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Column(
          children: [
            /// 顶部 TabBar，切换 Libraries / Download & Install
            TabBar(
              labelColor: cs.onSurface,
              unselectedLabelColor: cs.onSurface.withOpacity(0.5),
              indicatorSize: TabBarIndicatorSize.label,
              indicatorWeight: 2,
              dividerColor: Colors.grey.withOpacity(0.35),
              tabs: const [
                Tab(text: 'Libraries'),
                Tab(text: 'Download & Install'),
              ],
            ),
            const SizedBox(height: 12),

            /// 面板区域：随 Tab 切换不同内容
            Expanded(
              child: Card(
                elevation: 0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(6),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: TabBarView(
                    children: [
                      /// Libraries 面板
                      LibrariesTabFull(
                        installed: controller.data.installedSdkPaths,
                        current: controller.data.currentSdkPath,
                        onMakeCurrent: controller.setCurrentSdkPath,
                        onRemove: controller.removeInstalled,
                        onRevealInFinder: controller.revealInFileManager,
                      ),

                      /// Download & Install 面板
                      DownloadTab(
                        releases: controller.releases,
                        selected: controller.selectedRelease,
                        installDir: controller.installDir,
                        isDownloading: controller.isDownloading,
                        progress: controller.progress,
                        error: controller.downloadError,
                        onSelectRelease: controller.setSelectedRelease,
                        onPickInstallDir: () =>
                            controller.selectInstallDir(context: context),
                        onDownload: () =>
                            controller.downloadAndInstall(onSnack: (msg) {
                              final m = ScaffoldMessenger.maybeOf(context);
                              if (m != null) {
                                m.showSnackBar(SnackBar(content: Text(msg)));
                              }
                            }),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
